/*
 * @Author: ZhanXie
 * @Date: 2019-12-04 17:31:53
 * @LastEditors: ZhanXie
 * @LastEditTime: 2020-05-06 10:29:31
 * @Description: blog 路由
 * @FilePath: \projectStudy\node-demo\blog-express\routes\blog.js
 */
var express = require('express');
var router = express.Router();
const {
  getList,
  getDetail,
  newBlog,
  updateBlog,
  delBlog
} = require('../controller/blog')
const { SuccessModel, ErrorModel } = require('../model/resModel')
const loginCheck = require('../middleware/loginCheck')

router.get('/list', (req, res, next) => {
  let author = req.query.author || ''
  const keyword = req.query.keyword || ''
  const isadmin = req.query.isadmin || ''

  // 管理员界面
  if (isadmin) {
    if (req.session.username == null) {
      // 未登录
      res.json(new ErrorModel('未登录'))
      return
    }
    // 强制查询自己的博客
    author = req.session.username
  }

  const result = getList(author, keyword)
  return result.then(listData => res.json(new SuccessModel(listData)))
});

router.get('/detail', (req, res, next) => {
  const result = getDetail(req.query.id)
  return result.then(data => res.json(new SuccessModel(data)))
})

router.post('/new', loginCheck, (req, res, next) => {
  req.body.author = req.session.username
  const result = newBlog(req.body)
  return result.then(data => res.json(new SuccessModel(data)))
})

router.post('/update', loginCheck, (req, res, next) => {
  const result = updateBlog(req.query.id, req.body)
  return result.then(val => {
    if (val) {
      return res.json(new SuccessModel())
    } else {
      return res.json(new ErrorModel('更新博客失败'))
    }
  })
})

router.post('/del', loginCheck, (req, res, next) => {
  const author = req.session.username
  const result = delBlog(req.query.id, author)
  return result.then(val => {
    if (val) {
      return res.json(new SuccessModel())
    } else {
      return res.json(new ErrorModel('删除博客失败'))
    }
  })
})

module.exports = router;